import Request from '@/libs/request'
import qs from 'qs'
/**查询员工绩效列表 */
export const selectMyPm = (data: {userDDId: string}) =>
  Request.get<MyPm.Pm[]>('/pm/selectMyPm', data)

/**通过pmId ,查询绩效 */
export const selectMyPmById = (data: {pmId: number}) => {
  console.log('#查询绩效API', data)

  if (
    data.pmId == null ||
    data.pmId == undefined ||
    data.pmId == 'null' ||
    data.pmId === 'undefined'
  ) {
    return
  }

  return Request.get<MyPm.Pm>('/pm/selectMyPmById', data)
}

/**查询绩效数据 */
export const selectPmData = (data: {pmId: number}) =>
  Request.get<MyPm.PmData[]>('/pm/selectPmData', data, {successMessage: false})

/**查询绩效流程 */
export const selectPmProgress = (data: {pmId: number}) => {
  console.log('#查询绩效API', data)
  if (
    data.pmId == null ||
    data.pmId == undefined ||
    data.pmId == 'null' ||
    data.pmId === 'undefined'
  ) {
    return
  }
  return Request.get<MyPm.PmProgress[]>('/pmProgress/selectPmProgress', data)
}

/**管理员驳回流程-查询驳回流程 */
export const selectRebutProgress = (data: {userDDId: string; nowPmProgressId: number}) =>
  Request.get<MyPm.PmProgress[]>('/pmProgress/selectRebutProgress', data)

/**管理员驳回流程-查询驳回流程 */
export const rebutPmProgress = (data: {
  makeUserDDId: string
  nowPmProgressId: number
  rebutPmProgressId: number
  rebutCause?: string
}) => Request.get<null>('/pmProgress/rebutPmProgress', data)

/**查询流程数据 */
export const selectPmProgressById = (data: {pmProgressId: number}) =>
  Request.get<MyPm.PmProgress>('/pmProgress/selectPmProgressById', data)

/**绩效指标 */
export interface DimensionTarget
  extends Pick<
    MyPm.PmTarget,
    | 'pmDimensionId'
    | 'pmTargetId'
    | 'pmTargetName'
    | 'pmTargetStand'
    | 'pmTargetWeight'
    | 'pmTargetRange'
    | 'sort'
    | 'isBonus'
    | 'isMustSelect'
  > {
  appointScoreSign: 0 | 1
  makeProperty: 'add' | 'edit' | 'delete' | undefined
  appointUserDDId: string | undefined
}

/**绩效维度 */
export interface Dimension extends Omit<MyPm.PmData, 'pmId' | 'pmDimensionId' | 'pmTargetList'> {
  pmDimensionId: number
  pmTargetList: DimensionTarget[]
}

/**目标制定 暂存 */
export const temporarySavePm = (data: {
  pmProgressId: number
  data: Dimension[]
  uploadFileData?: File | undefined
}) => {
  return Promise.reject(false)
  // @ts-ignore
  data.data = JSON.stringify(data.data)
  return Request.post<null>(
    '/pm/submitPm',
    qs.stringify(Object.assign({makeType: 'temporarySave'}, data)),
    {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      }
    }
  )
}

/**目标制定 保存 */
export const submitPm = (data: {
  pmProgressId: number
  data: Dimension[]
  uploadFileData?: File | undefined
  scoreSummary?: string
}) => {
  let body: {
    pmProgressId: number
    data: string
    uploadFileData?: File | undefined
  } = {
    pmProgressId: data.pmProgressId,
    data: JSON.stringify(data.data),
    uploadFileData: data.uploadFileData || undefined
  }
  console.log(qs.stringify(Object.assign({makeType: 'submit'}, body)))
  return Request.post<null>(
    '/pm/submitPm',
    qs.stringify(Object.assign({makeType: 'submit'}, body)),
    {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      }
    }
  )
}

export type ScoreSubmitParamData = {
  makeUserDDId: string
  makeUserName: string
  pmProgressId: number
  pmTargetId: number
  score: number
  scoreContent: string
  scoreDesc: string
}
export type AnnexData = {
  annexFileExtension: string
  annexFileId: string
  annexFileName: string
  annexFileSize: number
  annexFileSpaceId: string
}
export type ScoreSubmitParam = {
  pmProgressId: number
  data: ScoreSubmitParamData[]
  uploadFileData?: File | undefined
  scoreSummary?: string
  annexData?: AnnexData[]
}
/**自评 保存 */
export const scoreBySelfSubmit = (data: ScoreSubmitParam) => {
  let body: {
    pmProgressId: number
    data: string
    uploadFileData?: File | undefined
    scoreSummary?: string
    annexData?: string
  } = {
    pmProgressId: data.pmProgressId,
    data: JSON.stringify(data.data),
    uploadFileData: data.uploadFileData || undefined,
    scoreSummary: data.scoreSummary,
    annexData: JSON.stringify(data.annexData)
  }

  return Request.post<null>(
    '/pm/submitPm',
    qs.stringify(Object.assign({makeType: 'submit'}, body)),
    {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      }
    }
  )
}

/**自评 暂存 */
export const scoreBySelfTemporarySave = (data: ScoreSubmitParam) => {
  let body: {
    pmProgressId: number
    data: string
    uploadFileData?: File | undefined
  } = {
    pmProgressId: data.pmProgressId,
    data: JSON.stringify(data.data),
    uploadFileData: data.uploadFileData || undefined
  }

  return Request.post<null>(
    '/pm/submitPm',
    qs.stringify(Object.assign({makeType: 'temporarySave'}, body)),
    {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      }
    }
  )
}

/**领导评分 保存 */
export const scoreByLeaderSubmit = (data: ScoreSubmitParam) => {
  let body: {
    pmProgressId: number
    data: string
    uploadFileData?: File | undefined
    scoreSummary?: string
    annexData?: string
  } = {
    pmProgressId: data.pmProgressId,
    data: JSON.stringify(data.data),
    uploadFileData: data.uploadFileData || undefined,
    scoreSummary: data.scoreSummary,
    annexData: JSON.stringify(data.annexData)
  }

  return Request.post<null>(
    '/pm/submitPm',
    qs.stringify(Object.assign({makeType: 'submit'}, body)),
    {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      }
    }
  )
}
